package cn.cdu.doctor.controller;

import cn.cdu.basics.baseVo.PageVo;
import cn.cdu.basics.baseVo.Result;
import cn.cdu.basics.utils.PageUtil;
import cn.cdu.basics.utils.ResultUtil;
import cn.cdu.data.utils.NullUtils;
import cn.cdu.doctor.entity.AutoSchedulingConfig;
import cn.cdu.doctor.service.IAutoSchedulingConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

@RestController
@Api(tags = "自动放号配置")
@RequestMapping("/zwz/autoScheduling")
@Transactional
public class AutoSchedulingConfigController {

    @Autowired
    private IAutoSchedulingConfigService autoSchedulingConfigService;

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取自动放号配置")
    public Result<IPage<AutoSchedulingConfig>> getByPage(@ModelAttribute AutoSchedulingConfig config, @ModelAttribute PageVo page) {
        QueryWrapper<AutoSchedulingConfig> qw = new QueryWrapper<>();
        if (!NullUtils.isNull(config.getDoctorId())) {
            qw.eq("doctor_id", config.getDoctorId());
        }
        if (!NullUtils.isNull(config.getDoctorName())) {
            qw.like("doctor_name", config.getDoctorName());
        }
        return new ResultUtil<IPage<AutoSchedulingConfig>>().setData(autoSchedulingConfigService.page(PageUtil.initMpPage(page), qw));
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改自动放号配置")
    public Result<AutoSchedulingConfig> saveOrUpdate(@RequestBody AutoSchedulingConfig config) {
        if (autoSchedulingConfigService.saveOrUpdate(config)) {
            return new ResultUtil<AutoSchedulingConfig>().setData(config);
        }
        return ResultUtil.error("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除自动放号配置")
    public Result<Object> delByIds(@RequestParam String[] ids) {
        for (String id : ids) {
            autoSchedulingConfigService.removeById(id);
        }
        return ResultUtil.success("删除成功");
    }

    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ApiOperation(value = "更新自动放号配置状态")
    public Result<Object> updateStatus(@RequestParam String id, @RequestParam Integer enabled) {
        AutoSchedulingConfig config = new AutoSchedulingConfig();
        config.setId(id);
        config.setEnabled(enabled);
        if (autoSchedulingConfigService.updateById(config)) {
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.error("更新失败");
    }
} 